package list_map;

import java.util.*;

/**
 * @program: javase
 * @description:
 * @author: lld
 * @create: 2020-11-05 20:00
 **/
public class map {
    public static void main(String[] args) {
        /** key,value遍历 */
        Map<String,String> hashMap=new HashMap<>();
        //key无序，不可重复，使用set存储
        //value无序，可重复，使用collection存储
        //一个键值对构成一个entry
        hashMap.put("1", "11");
        hashMap.put("1", "111");
        hashMap.put("2", "11221");
        hashMap.put("3", "112221221");

        Set<String> keys = hashMap.keySet();
        System.out.println("遍历keys"+keys);
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        Collection<String> values = hashMap.values();
        System.out.println("遍历values"+values);
        for (Object v:values) {
            System.out.println(v);
        }
        System.out.println("通过entrySet()获取entry<k,v>");
        Set<Map.Entry<String, String>> entrySet = hashMap.entrySet();
        for(Map.Entry<String, String> entry:entrySet){
            System.out.println("key："+entry.getKey()+": value："+entry.getValue());
        }
//        Iterator iterator1 = entrySet.iterator();
//        while (iterator1.hasNext()){
//          Map.Entry entry = (Map.Entry) iterator1.next();
//            System.out.println(entry.getKey()+":"+entry.getValue());
//        }

        Map<Integer,Integer> map=new HashMap<>();
        map.putIfAbsent(1, 2);
        map.putIfAbsent(1, 3);//空才覆盖
        Integer orDefault = map.getOrDefault(2, 0);
        System.out.println("orDefault = " + orDefault);
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        for(Map.Entry<Integer,Integer> entry:entries){
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
        /**直接输出map,调用map中key,value的toString(),用=连接  */
        HashMap hashMap1 = new HashMap();
        hashMap1.put(1,1);
        hashMap1.put(2,2);
        System.out.println(hashMap1);


        String sss="acd";
        /**这里的hashCode()当成hashmap中hash()方法计算出来的值 */
        System.out.println("sss.hashCode() = " + sss.hashCode());
//        System.out.println("sss1.hashCode() = " + sss1.hashCode());

        System.out.println("sss.hashCode()&(16-1) = " + (sss.hashCode() & (16-1)));
        System.out.println("sss.hashCode()%16 = " + sss.hashCode() % 16);
        //模拟扩容为32
//        桶位置
        System.out.println("sss.hashCode()&(32-1) = " + (sss.hashCode() & (32 - 1)));
        System.out.println("sss.hashCode()&16 = " + (sss.hashCode() &16));

        String sss1="hsahbkjyswe";
        //key.hash()&oldCap!=0
        System.out.println("sss1.hashCode()&16 = " + (sss1.hashCode() &16));
        //oldIndex
        System.out.println("sss1.hashCode()&(16-1) = " + (sss1.hashCode() & (16-1)));
        //newIndex=oldIndex+oldCap
        System.out.println("sss1.hashCode()&(32-1) = " + (sss1.hashCode() & (32 - 1)));

        System.out.println(Integer.MAX_VALUE);
        System.out.println(Math.pow(2, 31)-1);
        System.out.println((-22&2147483647)%16);

        Hashtable<String,String> hashtable=new Hashtable<>();
//        hashtable.put(null,"s");//key,value都不能为null
        hashtable.put("23", "34");
        hashtable.put("22", "34");
        System.out.println(hashtable);

        Enumeration<String> elements = hashtable.elements();
        if(elements.hasMoreElements()){
            System.out.println(elements.nextElement());
        }
        /* hashmap key,value可以为null*/
        HashMap<String,String> hashMap2=new HashMap<>();
        hashMap2.put(null,"s");
        hashMap2.put(null,"s3");
        hashMap2.put("111",null);
        System.out.println(hashMap2.get(null));
        System.out.println(hashMap2.get("111"));//null
        System.out.println(hashMap2.get("222"));//不存在 ,null
        System.out.println(hashMap2.containsKey("111"));//存在
        System.out.println(hashMap2.containsKey("222"));//不存在

//        System.out.println(0);
    }
}
